package com.phone580.app.untilty;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.WeakHashMap;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Debug;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.util.Log;

import com.phone580.app.R;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.entity.RunningAppInfoEntity;

public class CommonUtility {
	private static final String SCHEME = "package";
	/**
	 * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)
	 */
	private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
	/**
	 * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)
	 */
	private static final String APP_PKG_NAME_22 = "pkg";
	/**
	 * InstalledAppDetails所在包名
	 */
	private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
	/**
	 * InstalledAppDetails类名
	 */
	private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";

	/**
	 * 调用系统InstalledAppDetails界面显示已安装应用程序的详细信息。 对于Android 2.3（Api Level
	 * 9）以上，使用SDK提供的接口； 2.3以下，使用非公开的接口（查看InstalledAppDetails源码）。
	 * 
	 * @param context
	 * 
	 * @param packageName
	 *            应用程序的包名
	 */
	public static void showInstalledAppDetails(Context context,
			String packageName) {
		Intent intent = new Intent();
		final int apiLevel = Build.VERSION.SDK_INT;
		if (apiLevel >= 9) { // 2.3（ApiLevel 9）以上，使用SDK提供的接口
			intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
			Uri uri = Uri.fromParts(SCHEME, packageName, null);
			intent.setData(uri);
		} else { // 2.3以下，使用非公开的接口（查看InstalledAppDetails源码）
			// 2.2和2.1中，InstalledAppDetails使用的APP_PKG_NAME不同。
			final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
					: APP_PKG_NAME_21);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setClassName(APP_DETAILS_PACKAGE_NAME,
					APP_DETAILS_CLASS_NAME);
			intent.putExtra(appPkgName, packageName);
		}
		 context.startActivity(intent);
//		((Activity) context).startActivityForResult(intent, 1);
	}

    public static boolean isWiFiActive(Context inContext) {  
        Context context = inContext.getApplicationContext();  
        ConnectivityManager connectivity = (ConnectivityManager) context  
                .getSystemService(Context.CONNECTIVITY_SERVICE);  
        if (connectivity != null) {  
            NetworkInfo[] info = connectivity.getAllNetworkInfo();  
            if (info != null) {  
                for (int i = 0; i < info.length; i++) {  
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {  
                        return true;  
                    }  
                }  
            }  
        }  
        return false;  
    }  

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo network = cm.getActiveNetworkInfo();
		if (network != null) {
			return network.isAvailable();
		}
		return false;
	}

	public static DataInputStream Terminal(String command) throws Exception {
		Process process = Runtime.getRuntime().exec("su");
		// 执行到这，Superuser会跳出来，选择是否允许获取最高权限
		OutputStream outstream = process.getOutputStream();
		DataOutputStream DOPS = new DataOutputStream(outstream);
		InputStream instream = process.getInputStream();
		DataInputStream DIPS = new DataInputStream(instream);
		String temp = command + "\n";
		// 加回车
		DOPS.writeBytes(temp);
		// 执行
		DOPS.flush();
		// 刷新，确保都发送到outputstream
		DOPS.writeBytes("exit\n");
		// 退出
		DOPS.flush();
		process.waitFor();
		return DIPS;
	}

	public static boolean isRooted() {
		// 检测是否ROOT过
		DataInputStream stream;
		boolean flag = false;
		try {
			stream = Terminal("ls /data/");
			// 目录哪都行，不一定要需要ROOT权限的
			if (stream.readLine() != null)
				flag = true;
			// 根据是否有返回来判断是否有root权限
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();

		}

		return flag;
	}

	// 执行linux命令并且输出结果
	public static Vector execRootCmd(String paramString) {
		Vector localVector = new Vector();
		try {
			Process localProcess = Runtime.getRuntime().exec("su ");// 经过Root处理的android系统即有su命令
			OutputStream localOutputStream = localProcess.getOutputStream();
			DataOutputStream localDataOutputStream = new DataOutputStream(
					localOutputStream);
			InputStream localInputStream = localProcess.getInputStream();
			DataInputStream localDataInputStream = new DataInputStream(
					localInputStream);
			String str1 = String.valueOf(paramString);
			String str2 = str1 + "\n";
			localDataOutputStream.writeBytes(str2);
			localDataOutputStream.flush();
			String str3 = localDataInputStream.readLine();
			localVector.add(str3);
			localDataOutputStream.writeBytes("exit\n");
			localDataOutputStream.flush();
			localProcess.waitFor();
			return localVector;
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return null;
	}

	// 执行linux命令但不关注结果输出
	public static void execRootCmdSilent(String paramString) {
		try {
			Process localProcess = Runtime.getRuntime().exec("su");
			Object localObject = localProcess.getOutputStream();
			DataOutputStream localDataOutputStream = new DataOutputStream(
					(OutputStream) localObject);
			String str = String.valueOf(paramString);
			localObject = str + "\n";
			localDataOutputStream.writeBytes((String) localObject);
			localDataOutputStream.flush();
			localDataOutputStream.writeBytes("exit\n");
			localDataOutputStream.flush();
			localProcess.waitFor();
			localObject = localProcess.exitValue();
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public static String getSystemMountStatus() {
		try {
			Process localProcess = Runtime.getRuntime().exec("su ");// 经过Root处理的android系统即有su命令
			OutputStream localOutputStream = localProcess.getOutputStream();
			DataOutputStream localDataOutputStream = new DataOutputStream(
					localOutputStream);
			InputStream localInputStream = localProcess.getInputStream();
			DataInputStream localDataInputStream = new DataInputStream(
					localInputStream);
			String cmd = "mount\n";
			localDataOutputStream.write(cmd.getBytes());
			localDataOutputStream.flush();
			String status = "";
			while (true) {
				String info = localDataInputStream.readLine();
				if (info.contains("/system")) {
					status = info;
					break;
				}
			}
			return status;
		} catch (Exception localException) {
			localException.printStackTrace();
		}
		return null;
	}

	public static void storeValue(Context context, String key, String value) {

		SharedPreferences sp = context.getSharedPreferences("phone580", 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static String getStoreValue(Context context, String key) {
		SharedPreferences sp = context.getSharedPreferences("phone580", 0);
		return sp.getString(key, null);
	}
	
	public static void storeIcon(Context context, String key, byte[] value) {
		SharedPreferences sp = context.getSharedPreferences("phone580_data", 0);
		SharedPreferences.Editor editor = sp.edit();
		String temp = MyBase64.encode(value, 0, value.length);
		editor.putString(key, temp);
		editor.commit();
	}
	
//	private static WeakHashMap<String, Bitmap> cache = new WeakHashMap<String, Bitmap>();
	private static Map<String, WeakReference<Bitmap>> cache = new HashMap<String, WeakReference<Bitmap>>();
	public static Bitmap getIcon(Context context, String key) {
		if (cache.containsKey(key)) {
			Bitmap bm = cache.get(key).get();
			if (bm != null) {
				return bm;
			}
		}
		SharedPreferences sp = context.getSharedPreferences("phone580_data", 0);
		String temp = sp.getString(key, null);
		byte[] b = MyBase64.decode(temp);
		if (b == null) {
			Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
			cache.put(key, new WeakReference<Bitmap>(bm));
			return bm;
		} else {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		}
	}

	public static String getAvailSDCard(Context context) {

		File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		String avail_sd = Formatter.formatFileSize(context, blockSize
				* availableBlocks);
		return avail_sd;
	}
	
	public static String getTotalSDCardSize(Context context) {
		File path = Environment.getExternalStorageDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long blockCount = stat.getBlockCount();
		String total_sd = Formatter.formatFileSize(context, blockSize
				* blockCount);
		return total_sd;
	}

	/*
	 * 获取可用rom大小
	 */
	public static String getAvailRom(Context context) {

		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		String avail_rom = Formatter.formatFileSize(context, blockSize
				* availableBlocks);
		return avail_rom;
	}

	/*
	 * 获取rom总大小
	 */
	public static String getTotalRomSize(Context context) {

		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		String total_rom = Formatter.formatFileSize(context, blockSize
				* totalBlocks);
		return total_rom;
	}
	
	/**
	 * 返回当前程序版本名
	 */
	public static String getAppVersionName(Context context) {
		String versionName = "";
		try {
			// 获取packagemanager的实例
			PackageManager packageManager = context.getPackageManager();
			// getPackageName()是你当前类的包名，0代表是获取版本信息
			PackageInfo packInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			versionName = packInfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}  
	
	public static List<AppInfoEntity> getSysAppList(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			// 判断是否为非系统预装的应用程序
			if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
				// system applications
				apps.add(pak);
			}
		}

		List<AppInfoEntity> data = new ArrayList<AppInfoEntity>();
		for (PackageInfo pkg : apps) {
			String label = pManager.getApplicationLabel(pkg.applicationInfo)
					.toString();
			String pkgName = pkg.packageName;
			String version = pkg.versionName;
			Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
			String srcPath = pkg.applicationInfo.sourceDir;
			System.out.println(srcPath);
			File file = new File(srcPath);
			long size = file.length();
			AppInfoEntity info = new AppInfoEntity();
			info.setAppLabel(label);
			info.setIcon(icon);
			info.setPkgName(pkgName);
			info.setVersion(version);
			info.setSize(size);
			info.setApkPath(srcPath);
			data.add(info);
		}
		return data;
	}
	
	public static List<AppInfoEntity> getMyAppList(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			// 判断是否为非系统预装的应用程序
			if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				// customs applications
				apps.add(pak);
			}
		}

		List<AppInfoEntity> data = new ArrayList<AppInfoEntity>();
		for (PackageInfo pkg : apps) {
			String label = pManager.getApplicationLabel(pkg.applicationInfo)
					.toString();
			String pkgName = pkg.packageName;
			String version = pkg.versionName;
			Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
			String srcPath = pkg.applicationInfo.sourceDir;
			File file = new File(srcPath);
			long size = file.length();
			AppInfoEntity info = new AppInfoEntity();
			info.setAppLabel(label);
			info.setIcon(icon);
			info.setPkgName(pkgName);
			info.setVersion(version);
			info.setSize(size);
			data.add(info);
		}
		return data;
	}
	
	public static List<AppInfoEntity> getAppOnPhoneList(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			// 判断是否为非系统预装的应用程序
			if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				// customs applications
				apps.add(pak);
			}
		}

		List<AppInfoEntity> data = new ArrayList<AppInfoEntity>();
		for (PackageInfo pkg : apps) {
			String label = pManager.getApplicationLabel(pkg.applicationInfo)
					.toString();
			String pkgName = pkg.packageName;
			String version = pkg.versionName;
			Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
			String srcPath = pkg.applicationInfo.sourceDir;
			File file = new File(srcPath);
			long size = file.length();
			AppInfoEntity info = new AppInfoEntity();
			info.setAppLabel(label);
			info.setIcon(icon);
			info.setPkgName(pkgName);
			info.setVersion(version);
			info.setSize(size);
			
			//判断安装位置
			if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
				info.setInstallAtSDCard(true);
				continue;
			} else {
				info.setInstallAtSDCard(false);
				data.add(info);
			}
		}
		return data;
	}

	public static List<AppInfoEntity> getAppOnSDCardList(Context context) {
		 List<PackageInfo> apps = new ArrayList<PackageInfo>();
			PackageManager pManager = context.getPackageManager();
			// 获取手机内所有应用
			List<PackageInfo> paklist = pManager.getInstalledPackages(0);
			for (int i = 0; i < paklist.size(); i++) {
				PackageInfo pak = (PackageInfo) paklist.get(i);
				// 判断是否为非系统预装的应用程序
				if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
					// customs applications
					apps.add(pak);
				}
			}

			List<AppInfoEntity> data = new ArrayList<AppInfoEntity>();
			for (PackageInfo pkg : apps) {
				String label = pManager.getApplicationLabel(pkg.applicationInfo)
						.toString();
				String pkgName = pkg.packageName;
				String version = pkg.versionName;
				Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
				String srcPath = pkg.applicationInfo.sourceDir;
				File file = new File(srcPath);
				long size = file.length();
				AppInfoEntity info = new AppInfoEntity();
				info.setAppLabel(label);
				info.setIcon(icon);
				info.setPkgName(pkgName);
				info.setVersion(version);
				info.setSize(size);
				//判断安装位置
				if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
					info.setInstallAtSDCard(true);
					data.add(info);
				} else {
					info.setInstallAtSDCard(false);
					continue;
				}
			}
		return data;
	}
	
	public static List<RunningAppInfoEntity> getRunningApps (Context context) {
		// 查询所有已经安装的应用程序
//		List<ApplicationInfo> listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
//		Collections.sort(listAppcations,new ApplicationInfo.DisplayNameComparator(pm));// 排序
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			// 判断是否为非系统预装的应用程序
			if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				// customs applications
				apps.add(pak);
			}
		}

		// 保存所有正在运行的包名 以及它所在的进程信息
		Map<String, ActivityManager.RunningAppProcessInfo> pgkProcessAppMap = new HashMap<String, ActivityManager.RunningAppProcessInfo>();

		ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		// 通过调用ActivityManager的getRunningAppProcesses()方法获得系统里所有正在运行的进程
		List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager
				.getRunningAppProcesses();

		for (ActivityManager.RunningAppProcessInfo appProcess : appProcessList) {
//			int pid = appProcess.pid; // pid
//			String processName = appProcess.processName; // 进程名

			String[] pkgNameList = appProcess.pkgList; // 获得运行在该进程里的所有应用程序包

			// 输出所有应用程序的包名
			for (int i = 0; i < pkgNameList.length; i++) {
				String pkgName = pkgNameList[i];
				// 加入至map对象里
				pgkProcessAppMap.put(pkgName, appProcess);
			}
		}
		// 保存所有正在运行的应用程序信息
		List<RunningAppInfoEntity> runningAppInfos = new ArrayList<RunningAppInfoEntity>(); // 保存过滤查到的AppInfo

		for (PackageInfo pkgInfo : apps) {
			// 如果该包名存在 则构造一个RunningAppInfo对象
			if (pgkProcessAppMap.containsKey(pkgInfo.packageName)) {
				// 获得该packageName的 pid 和 processName
				int pid = pgkProcessAppMap.get(pkgInfo.packageName).pid;
				String processName = pgkProcessAppMap.get(pkgInfo.packageName).processName;
				runningAppInfos.add(getAppInfo(context, pkgInfo.applicationInfo, pid, processName));
			}
		}
		return runningAppInfos;
	}
	
	public static RunningAppInfoEntity getAppInfo(Context context, ApplicationInfo app, int pid, String processName) {
		PackageManager pm = context.getPackageManager();
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		RunningAppInfoEntity appInfo = new RunningAppInfoEntity();
		appInfo.setAppLabel((String) app.loadLabel(pm));
		appInfo.setAppIcon(app.loadIcon(pm));
		appInfo.setPkgName(app.packageName);

		appInfo.setPid(pid);
		appInfo.setProcessName(processName);
		
		int[] myMempid = new int[] { pid };
		// 此MemoryInfo位于android.os.Debug.MemoryInfo包中，用来统计进程的内存信息
		Debug.MemoryInfo[] memoryInfo =  am.getProcessMemoryInfo(myMempid);
		// 获取进程占内存用信息 kb单位
		int memSize = memoryInfo[0].dalvikPrivateDirty;
		appInfo.setMemSize(memSize);
		return appInfo;
	}
	
	//sd卡是否可用
	 public static boolean isSDCardAvailable() {
	        if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
	           return true;
	       else
	           return false;
	    }
	 
	 public static Drawable getUninstallAPKIcon(Context context, String apkPath) {  
	        String PATH_PackageParser = "android.content.pm.PackageParser";  
	        String PATH_AssetManager = "android.content.res.AssetManager";  
	        try {  
	            // apk包的文件路径  
	            // 这是一个Package 解释器, 是隐藏的  
	            // 构造函数的参数只有一个, apk文件的路径  
	            // PackageParser packageParser = new PackageParser(apkPath);  
	            Class pkgParserCls = Class.forName(PATH_PackageParser);  
	            Class[] typeArgs = new Class[1];  
	            typeArgs[0] = String.class;  
	            Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);  
	            Object[] valueArgs = new Object[1];  
	            valueArgs[0] = apkPath;  
	            Object pkgParser = pkgParserCt.newInstance(valueArgs);  
	            Log.d("ANDROID_LAB", "pkgParser:" + pkgParser.toString());  
	            // 这个是与显示有关的, 里面涉及到一些像素显示等等, 我们使用默认的情况  
	            DisplayMetrics metrics = new DisplayMetrics();  
	            metrics.setToDefaults();  
	            // PackageParser.Package mPkgInfo = packageParser.parsePackage(new  
	            // File(apkPath), apkPath,  
	            // metrics, 0);  
	            typeArgs = new Class[4];  
	            typeArgs[0] = File.class;  
	            typeArgs[1] = String.class;  
	            typeArgs[2] = DisplayMetrics.class;  
	            typeArgs[3] = Integer.TYPE;  
	            Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage",  
	                    typeArgs);  
	            valueArgs = new Object[4];  
	            valueArgs[0] = new File(apkPath);  
	            valueArgs[1] = apkPath;  
	            valueArgs[2] = metrics;  
	            valueArgs[3] = 0;  
	            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);  
	            // 应用程序信息包, 这个公开的, 不过有些函数, 变量没公开  
	            // ApplicationInfo info = mPkgInfo.applicationInfo;  
	            Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");  
	            ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);  
	            // uid 输出为"-1"，原因是未安装，系统未分配其Uid。  
	            Log.d("ANDROID_LAB", "pkg:" + info.packageName + " uid=" + info.uid);  
	            // Resources pRes = getResources();  
	            // AssetManager assmgr = new AssetManager();  
	            // assmgr.addAssetPath(apkPath);  
	            // Resources res = new Resources(assmgr, pRes.getDisplayMetrics(),  
	            // pRes.getConfiguration());  
	            Class assetMagCls = Class.forName(PATH_AssetManager);  
	            Constructor assetMagCt = assetMagCls.getConstructor((Class[]) null);  
	            Object assetMag = assetMagCt.newInstance((Object[]) null);  
	            typeArgs = new Class[1];  
	            typeArgs[0] = String.class;  
	            Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath",  
	                    typeArgs);  
	            valueArgs = new Object[1];  
	            valueArgs[0] = apkPath;  
	            assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);  
	            Resources res = context.getResources();  
	            typeArgs = new Class[3];  
	            typeArgs[0] = assetMag.getClass();  
	            typeArgs[1] = res.getDisplayMetrics().getClass();  
	            typeArgs[2] = res.getConfiguration().getClass();  
	            Constructor resCt = Resources.class.getConstructor(typeArgs);  
	            valueArgs = new Object[3];  
	            valueArgs[0] = assetMag;  
	            valueArgs[1] = res.getDisplayMetrics();  
	            valueArgs[2] = res.getConfiguration();  
	            res = (Resources) resCt.newInstance(valueArgs);  
	            CharSequence label = null;  
	            if (info.labelRes != 0) {  
	                label = res.getText(info.labelRes);  
	            }  
	            // if (label == null) {  
	            // label = (info.nonLocalizedLabel != null) ? info.nonLocalizedLabel  
	            // : info.packageName;  
	            // }  
	            Log.d("ANDROID_LAB", "label=" + label);  
	            // 这里就是读取一个apk程序的图标  
	            if (info.icon != 0) {  
	                Drawable icon = res.getDrawable(info.icon);  
	               return icon;
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }
			return null;
	    }  
}
